package com.sqly.doc.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Environment;

public class CreateBitmap {
	
	public static String saveMyBitmap(Bitmap mBitmap)  {
		String picname= UUID.randomUUID().toString().replace("-","");
        File f = new File( Environment.getExternalStorageDirectory()+"/"+picname+".jpg");
        FileOutputStream fOut = null;
        try {
                fOut = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
                e.printStackTrace();
                return  null;
        }
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        try {
                fOut.flush();

        } catch (IOException e) {
                e.printStackTrace();
                return null;
        }
        try {
                fOut.close();
			return picname;
        } catch (IOException e) {
                e.printStackTrace();
                return null;
        }
}
	public static Bitmap zoomImg(Bitmap bm, int newWidth, int newHeight) {
		int width = bm.getWidth();
		int height = bm.getHeight();
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		Matrix matrix = new Matrix();
		float w=0.7f,h=0.7f;
		matrix.postScale(w, h);
		Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
				true);
		return newbm;
	}
	public static Bitmap addBitmaps(Bitmap first, Bitmap second,boolean d) {
//		if(d)
//		first=Bitmap.createBitmap(first,(int) Math.floor(first.getWidth()*0.1),0,(int) Math.floor(first.getWidth()*0.80),first.getHeight(), null,true);
//		second=Bitmap.createBitmap(second,(int) Math.floor(second.getWidth()*0.1),0,(int) Math.floor(second.getWidth()*0.80),second.getHeight(), null,true);
		int width = first.getWidth() + second.getWidth();
		int height = Math.max(first.getHeight(), second.getHeight());
		Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Canvas canvas = new Canvas(result);
		canvas.drawColor(Color.WHITE);
		canvas.drawBitmap(first, 0, 0, null);
		canvas.drawBitmap(second,first.getWidth(), 0, null);
		return result;
	}
	public static Bitmap addBitmapss(Bitmap first, Bitmap second,boolean d) {
		int width = first.getWidth() + second.getWidth();
		int height = Math.max(first.getHeight(), second.getHeight());
		Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Canvas canvas = new Canvas(result);
		canvas.drawColor(Color.WHITE);
		canvas.drawBitmap(first, 0, 0, null);
		canvas.drawBitmap(second,first.getWidth(), 0, null);
		return result;
	}
	public static Bitmap addBitmapsH(Bitmap first, Bitmap second) {
		int width = Math.max(first.getWidth() , second.getWidth());
		int height = first.getHeight()+ second.getHeight();
		Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Canvas canvas = new Canvas(result);
		canvas.drawColor(Color.WHITE);
		canvas.drawBitmap(first, 0, 0, null);
		canvas.drawBitmap(second, 0, first.getHeight(), null);
		return result;
	}
	public static Bitmap sign(Bitmap first, Bitmap second) {
		int width = Math.max(first.getWidth() , second.getWidth());
		int height = first.getHeight()+ second.getHeight();
		Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Canvas canvas = new Canvas(result);
		canvas.drawColor(Color.WHITE);
		canvas.drawBitmap(first, 0, 0, null);
		canvas.drawBitmap(second, first.getWidth()-second.getWidth(), first.getHeight(), null);
		return result;
	}
}
